//
//  BindAccountController.swift
//  SCA
//
//  Created by tjun on 2025/9/6.
//

import UIKit


class BindAccountController: BaseController {
    
    private var scrollView: UIScrollView!
    private var contentView: UIView!
    private let usernameTextField = LabeledTextField()
    private let otpTextField = LabeledTextField(tip: "请输入绑定号2FA验证码".localizedString)
    private let confirmButton = MainButton(type: .system, title: "确认".localizedString, font: UIFontMake(16), textColor: .white)

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.title = "绑定账号".localizedString
        setupUI()
    }
    
    private func setupUI() {
        
        scrollView = UIScrollView(frame: CGRect(x: 0, y: navBarHeight, width: view.width, height: screenHeight - navBarHeight)).addTo(view)
        scrollView.keyboardDismissMode = .onDrag
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = true
        scrollView.contentInsetAdjustmentBehavior = .never
        
        let contentSize = CGSize(width: scrollView.width, height: scrollView.height)
        
        scrollView.contentSize = contentSize
        
        contentView = UIView().addTo(scrollView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.size.equalTo(contentSize)
        }
 
        usernameTextField.titleAttributedText = "绑定用户名*".localizedString.attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        contentView.addSubview(usernameTextField)
        usernameTextField.textField.keyboardType = .asciiCapable
        usernameTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        usernameTextField.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
        }
 
        otpTextField.titleAttributedText = "2FA OTP*".attributedString(rangeString: "*", rangeColor: UIColorMake(0xFF0000))
        contentView.addSubview(otpTextField)
        otpTextField.textField.addTarget(self, action: #selector(textFieldDidChange(_ :)), for: .editingChanged)
        otpTextField.snp.makeConstraints { make in
            make.top.equalTo(usernameTextField.snp.bottom).offset(10)
            make.left.right.equalTo(usernameTextField)
        }
        
        confirmButton.addTo(contentView)
        confirmButton.isEnabled = false
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        confirmButton.snp.makeConstraints { make in
            make.left.right.equalTo(usernameTextField)
            make.height.equalTo(AppStyle.size.buttonHeight.scaled)
            make.top.equalTo(otpTextField.snp.bottom).offset(50)
        }

    }

    @objc func textFieldDidChange(_ sender: UITextField) {
        if let username = usernameTextField.textField.text,
           let otp = otpTextField.textField.text,
           username.count > 0,
           otp.count > 0 {
            confirmButton.isEnabled = true
        } else {
            confirmButton.isEnabled = false
        }
    }
 
    @objc func confirmButtonTapped() {
        
        guard let username = usernameTextField.textField.text,
              let otp = otpTextField.textField.text else {
            return
        }
        
        view.endEditing(true)
        HUD.showLoadingInView(self.view)
        MineService.bindAccount(username: username, otf: otp) {
            HUD.hideHUD(for: self.view)
            HUD.showMessage("绑定成功".localizedString)
            self.navigationController?.popViewController(animated: true)
        }
    }
 
}

 

